// childPage/goodsList/goodsList.js
const request = require('../../util/request')
Page({
    /**
     * 页面的初始数据
     */
    data: {
        goodsList: [],
        total: 1,
        visable: false,
        query: {
            pageNum: 1,
            pageSize: 4,
            key: '',
            sorted: 0,
            brandIds: ''
        },
        brandList: [],
        currentSelect: {},
        selectState: {},
        brandIds: []
    },


    changeVisable(){
        this.setData({
            visable:false
        })
    },
    //选择品牌
    selectBrand(e) {
        let id = e.currentTarget.dataset.brand._id
        this.data.currentSelect[id] = !this.data.currentSelect[id]
        this.setData({
            selectState: this.data.currentSelect,
        })
        console.log(this.data.currentSelect[id])
        //将选中的数据添加到数组中
        if (this.data.currentSelect[id] == true) {
            this.data.brandIds.push(id)
            //取消选择就从数组中删除
        } else {
            let index = this.data.brandIds.indexOf(id);
            this.data.brandIds.splice(index, 1);
        }
        console.log(this.data.brandIds)
    },

    //确定按钮
    comfirm(e) {
        if (this.data.brandIds) {
            this.setData({
                'query.brandIds': this.data.brandIds.join(',')
            })
        }
        this.setData({
            visable: !this.data.visable
        })
        this.clear()
        this.getList()
    },

    //改变选中状态
    changeSorted(e) {
        this.clear()
        if (this.data.visable == true && this.data.query.sorted != 4) {
            this.data.currentSelect = {}
            this.data.query.brandIds.split(',').forEach(res =>{
                console.log('res',res)
                this.data.currentSelect.res = true
            })
            this.setData({
                selectState: this.data.currentSelect,
                visable: !this.data.visable
            })
        }
        this.setData({
            'query.sorted': e.currentTarget.dataset.sorted
        })
        this.getList()
    },


    //请空选择状态
    clear() {
        this.data.query.pageNum = 1,
            this.data.total = 0;
        this.setData({
            'query.key': '',
            goodsList: []
        })
    },


    //获取品牌
    getBrand() {
        request.get('/wx/getBrand').then((res) => {
            let brandList = res
            this.setData({
                brandList
            })
        })
    },
    //展示弹出品牌框
    showBrand(e) {
        this.changeSorted(e)
        this.setData({
            visable: !this.data.visable
        })

    },



    back() {
        wx.navigateBack({
            delta: 1,
        })
    },
    //传参跳转
    tap(e) {
        console.log(e.currentTarget.dataset.id)
        wx.navigateTo({
            url: '/childPage/goodsDetail/goodsDetail?productId=' + e.currentTarget.dataset.id,
        })
    },

    getList(key) {
        return new Promise((resolve, reject) => {
            if (key) {
                this.setData({
                    'query.key': key
                })
            }
            console.log(this.data.query)
            request.get('/wx/getProduct', this.data.query).then((res) => {
                this.setData({
                    total: res.total,
                    goodsList: [...this.data.goodsList, ...res.rows]
                })
                console.log(this.data.goodsList)
            })
            resolve()
        })
    },
    //输入搜索关键字
    searchByKey(e) {
        this.data.query.key = e.detail.value;
    },
    //重新加载数据
    refresh() {
        this.data.goodsList = []
        console.log('刷新：', this.data.query)
        this.data.query.pageNum = 1,
            this.data.total = 0;
        this.getList();
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    // 下拉刷新
    onPullDownRefresh: function () {
        this.data.goodsList = [],
            this.data.query.pageNum = 1,
            this.data.total = 0;
        this.getList().then(res => {
            wx.stopPullDownRefresh({
                success: (res) => {
                    console.log('刷新成功')
                },
            })
        })
    },
    /**
     * 页面上拉触底事件的处理函数，上拉加载
     */
    onReachBottom: function () {
        this.data.query.pageNum++;
        //先判断数组长度是否小于total
        if (this.data.goodsList.length < this.data.total) {
            this.getList()
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getList(options.key);
        this.getBrand()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },





    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})